package com.example.helloworld;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import com.example.helloworld.util.ActivityUtil;
import com.example.helloworld.util.JsonUtil;
import com.example.helloworld.util.ResourceUtil;

public class UseJson extends Activity {
	private static final String MENUS =	"menus"; 
	private static final String GROUP =	"group"; 
	private static final String CHILD =	"child";
	private static final String TITLE =	"title";
	private static final String CLASS =	"class";
	private static final String DEFAULT_OPEN = "defaultOpen";
	
	private List<Map<String, Object>> mGroupList =			new ArrayList<Map<String, Object>> ();	
	private List<List<Map<String, Object>>> mChildList =	new ArrayList<List<Map<String, Object>>> ();
	private ExpandableListAdapter mAdapter;
	private Map<String, Object> mDefaultOpen =				new HashMap<String, Object> ();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_use_json);
		
		String jsonStr = ResourceUtil.getRawStringResource(getApplicationContext(), R.raw.menus);
		TextView view = (TextView)findViewById(R.id.txt_json_content);
		view.setText(jsonStr);
		
		loadJson(jsonStr);
		
		mAdapter =	new SimpleExpandableListAdapter(
				this,
				mGroupList,
				R.layout.simple_expandable_list_item_1,
				new String[] {GROUP},
				new int[] {android.R.id.text1},
				mChildList,
				R.layout.simple_expandable_list_item_2,
				new String[] {TITLE, CLASS},
				new int[] {android.R.id.text1, android.R.id.text2}
		);
			
		ExpandableListView listView =	(ExpandableListView)findViewById(R.id.elst_menu);
		listView.setAdapter(mAdapter);
	}
	
	private void loadJson(String jsonStr) {
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			Map<String, Object> menus =	JsonUtil.getJsonRecord(jsonObject, null, new String[] {MENUS});
			JSONArray array = (JSONArray)menus.get(MENUS);
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonRecord = array.getJSONObject(i);
				Map<String, Object>	groupRecord = JsonUtil.getJsonRecord(jsonRecord, new String[] {GROUP}, new String[] {CHILD});
				String group = (String)groupRecord.get(GROUP);
				mGroupList.add(groupRecord);
				JSONArray childArray = (JSONArray)groupRecord.get(CHILD);
				
				List<Map<String, Object>> groupChildList =	new ArrayList<Map<String, Object>> ();
				for (int j = 0; j < childArray.length(); j++) {
					Map<String, Object>	childRecord = JsonUtil.getJsonRecord(childArray.getJSONObject(j), new String[] {TITLE, CLASS, DEFAULT_OPEN}, null);
					groupChildList.add(childRecord);
				}
				mChildList.add(groupChildList);
			}
			
			{
				Map<String, Object> defaultOpen = JsonUtil.getJsonRecord(jsonObject, null, null, new String[] {DEFAULT_OPEN});
				JSONObject jsonDefaultOpen = (JSONObject)defaultOpen.get(DEFAULT_OPEN);
				mDefaultOpen = JsonUtil.getJsonRecord(jsonDefaultOpen, new String[] {GROUP, CLASS});
				ActivityUtil.debug("default open child", mDefaultOpen.toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
